package theTVDB.com.command
{

	import com.adobe.cairngorm.control.CairngormEvent;
	
	import mx.rpc.AsyncToken;
	import mx.rpc.http.HTTPService;
	
	import theTVDB.com.event.GetSeriesEvent;
	import theTVDB.com.service.Services;
	import theTVDB.com.vo.SeriesVO;

	public class GetSeriesCommand extends CallbackCommand
	{

		private var services:Services = Services.getInstance();
		private var seriesID:String;

		override public function result(data:Object):void
		{
			var SearchResults:XML = (data.result as XML);
			var seriesData:XML;
			for each (seriesData in SearchResults..Series)
			{
				var seriesVO:SeriesVO = new SeriesVO();
				seriesVO.id = seriesData.id;
				seriesVO.actors = seriesData.Actors;
				seriesVO.airs_DayOfWeek = seriesData.Airs_DayOfWeek;
				seriesVO.airs_Time = seriesData.Airs_Time;
				seriesVO.contentRating = seriesData.ContentRating;
				seriesVO.firstAired = seriesData.FirstAired;
				seriesVO.genre = seriesData.Genre;
				seriesVO.IMDB_id = seriesData.IMDB_ID;
				seriesVO.language = seriesData.Language;
				seriesVO.network = seriesData.Network;
				seriesVO.overview = seriesData.Overview;
				seriesVO.rating = seriesData.Rating;
				seriesVO.runtime = seriesData.Runtime;
				seriesVO.seriesID = seriesData.SeriesID;
				seriesVO.seriesName = seriesData.SeriesName;
				seriesVO.status = seriesData.Status;
				seriesVO.banner = seriesData.banner;
				seriesVO.fanart = seriesData.fanart;
				seriesVO.lastUpdated = seriesData.lastupdated;
				seriesVO.zap2it_id = seriesData.zap2it_id;
			}

			if (callback != null)
				callback(seriesVO,seriesID);
		}

		override public function fault(info:Object):void
		{
			trace(info);
			
			if (callback != null)
				callback(null,seriesID);
		}

		override public function execute(event:CairngormEvent):void
		{
			callback = (event as GetSeriesEvent).callback;
			seriesID = (event as GetSeriesEvent).SeriesID;
			var tmpService:HTTPService = services.getSeries((event as GetSeriesEvent).SeriesID);
			var token:AsyncToken = tmpService.send();
			token.addResponder(this);
		}
	}
}
